package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddArticleDto;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/ydd_article")
public class YddArticleController {


    @Autowired
    private IYddArticleService yddArticleService;

    @Autowired
    private IYddCateService yddCateService;


    /**
     * 内容管理/文章管理-按id查询
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Long id) {
        log.info("内容管理/文章管理-按id查询 ...");

        return ResultData.ok("",yddArticleService.getById(id));
    }

    /**
     * 内容管理/文章管理-分页查询
     * @param start_time
     * @param end_time
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData page(Long start_time, Long end_time, String title, Integer cateid, Integer page, Integer limit){
        log.info("内容管理/文章管理-分页查询 ...");


        //创建分页构造器
        Page<YddArticle> yddTagPage = new Page<>(page, limit);

        //创建条件查询对象
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(title != null, YddArticle::getTitle, title);
        queryWrapper.eq(cateid != null, YddArticle::getCateid, cateid);
        queryWrapper.gt(start_time != null, YddArticle::getCreateDate, start_time)
//                .lt(end_time != null, YddArticle::getUpdateDate, end_time);
                .lt(end_time != null, YddArticle::getCreateDate, end_time);

        //时间排序
        queryWrapper.orderByDesc(YddArticle::getCreateDate).orderByDesc(YddArticle::getUpdateDate);


        //查询
        yddArticleService.page(yddTagPage, queryWrapper);

        Page<YddArticleDto> yddArticleDtoPage = new Page<>();
        //拷贝数据
        BeanUtils.copyProperties(yddTagPage, yddArticleDtoPage, "records");

        List<YddArticleDto> articleDtos = yddTagPage.getRecords().stream().map(article -> {
            YddArticleDto articleDto = new YddArticleDto();

            BeanUtils.copyProperties(article,articleDto);
            YddCate cate = yddCateService.getById(article.getCateid());

            if (cate!=null){
                articleDto.setCatename(cate.getCatename());

            }
            return articleDto;
        }).collect(Collectors.toList());
        yddArticleDtoPage.setRecords(articleDtos);
        return ResultData.ok("",PageResult.init(yddArticleDtoPage));

//        return ResultData.ok(PageResult.init(yddTagPage));
    }

    /**
     * 内容管理/文章管理-添加
     * @param yddArticle
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddArticle yddArticle) {
        log.info("内容管理/文章管理-添加 ..." + yddArticle);
        String[] split = yddArticle.getPic().split("/");
        yddArticle.setPic("/"+split[1]);
        //前端的日期传不过来
        yddArticle.setCreateDate(System.currentTimeMillis());
//        yddArticle.setUpdateDate(System.currentTimeMillis());

        yddArticleService.save(yddArticle);

        return ResultData.ok("添加成功",null);
    }

    /**
     * 内容管理/文章管理-修改
     * @param yddArticle
     * @return
     */
//    @PutMapping("/update")
    @PostMapping ("/update")
    public ResultData update(@RequestBody YddArticle yddArticle){
        log.info("内容管理/文章管理-修改 ...");
        String[] split = yddArticle.getPic().split("/");
        yddArticle.setPic("/"+split[1]);
        //前端的日期传不过来
//        yddArticle.setCreateDate(System.currentTimeMillis());
        yddArticle.setUpdateDate(System.currentTimeMillis());

        yddArticleService.updateById(yddArticle);

        return ResultData.ok("修改成功",null);
    }

    /**
     * 内容管理/文章管理-删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(Integer id) {
        log.info("内容管理/文章管理-删除 ...");

        yddArticleService.removeById(id);

        return ResultData.ok("删除成功",null);
    }


}
